package com.example.xianji.adapter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.xianji.R;

import com.example.xianji.bean.Place;
import com.example.xianji.bean.YiJi_Class;
import com.example.xianji.widgets.MyListview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ErJi_FuWuFenlei_Adapter extends BaseAdapter {
	/********************** 需求大厅 和服务大厅 分类 二级公用适配器 *************************************/
	private Context mContext;
	private ArrayList<YiJi_Class> fuwu_list;
	private int fenlei;

	private ArrayList<Place> place_list;
	private PlaceAdapter mPlaceAdapter;

	public ErJi_FuWuFenlei_Adapter(Context mContext,
			ArrayList<YiJi_Class> fuwu_list, int fenlei) {
		super();
		this.mContext = mContext;
		this.fuwu_list = fuwu_list;
		this.fenlei = fenlei;

	}

	@Override
	public int getCount() {
		return fuwu_list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder;
		if (convertView == null) {
			holder = new Holder();
			convertView = View.inflate(mContext, R.layout.place_listview_item,
					null);
			holder.linear_layout = (LinearLayout) convertView
					.findViewById(R.id.linear_layout);

			holder.place_text = (TextView) convertView
					.findViewById(R.id.place_text);
			holder.sanji_listview = (MyListview) convertView
					.findViewById(R.id.sanji_listview);

			convertView.setTag(holder);
		}
		holder = (Holder) convertView.getTag();

		holder.place_text.setText(fuwu_list.get(position).getName());
		if (fenlei == 1) {

			holder.place_text.setTextColor(mContext.getResources().getColor(
					R.color.blue22));
		} else {

			holder.place_text.setTextColor(mContext.getResources().getColor(
					R.color.gray75));

		}

		if (fenlei == 1) {
			// 为1 时表明从服务大厅分类点击的有三级列表

			place_list = new ArrayList<Place>();
			try {
				JSONArray array = fuwu_list.get(position).getCatethree();
				for (int i = 0; i < array.length(); i++) {

					JSONObject object;

					object = array.getJSONObject(i);

					Place mPlace = new Place();
					mPlace.setArea_id(object.getString("id"));
					mPlace.setArea_name(object.getString("name"));
					place_list.add(mPlace);

				}
				if (place_list.size() > 0) {
					holder.sanji_listview.setVisibility(View.VISIBLE);
					holder.sanji_listview.setAdapter(new PlaceAdapter(mContext,
							place_list, 1));

				} else {

					holder.sanji_listview.setVisibility(View.GONE);
				}

			} catch (JSONException e) {
				e.printStackTrace();
			}

			/*
			 * holder.sanji_listview.setOnItemClickListener(new
			 * OnItemClickListener() {
			 * 
			 * @Override public void onItemClick(AdapterView<?> arg0, View arg1,
			 * int arg2, long arg3) {
			 * 
			 * } });
			 */

		}

		return convertView;
	}

	class Holder {
		TextView place_text;
		LinearLayout linear_layout;
		MyListview sanji_listview;
	}

}
